package et.six.eta.View.Sideslip;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import java.util.ArrayList;
import java.util.List;
import et.six.eta.Adapter.ListAdapter_purch;
import et.six.eta.Ban.SideslipList;
import et.six.eta.MainActivity;
import et.six.eta.R;
import et.six.eta.View.ReflashListview;

public class purchase_record extends AppCompatActivity implements ReflashListview.IReflashListener{


    String food_name;
    String food_price;
    String food_img;

    ReflashListview lv;
    ArrayList<SideslipList> lxr;
    Button butt_food;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.purchase_record_activity);
        lv = findViewById(R.id.lv_zjmitem);
        lv.setInterface(this);
        butt_food=findViewById(R.id.butt_food);
        select();
        butt_food.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(purchase_record.this, MainActivity.class);
                startActivity(it);
            }
        });
    }



//
//        AVQuery<AVObject> query = new AVQuery<>("PurchaseRecord");
//        query.findInBackground(new FindCallback<AVObject>() {
//            @Override
//            public void done(List<AVObject> list, AVException e) {
//
//                    food_Detail = list.get(0).getString("food_detail");
//                    food_Name = list.get(0).getString("food_name");
//                    food_Price = list.get(0).getString("food_price");
//
//            }
//        });
//        food_name.setText(food_Name);
//        food_price.setText(food_Price);
//        food_detail.setText(food_Detail);



    public void select() {

        lxr = new ArrayList<SideslipList>();
        AVQuery<AVObject> query = new AVQuery<>("PurchaseRecord");
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                for (int i = 0; i < list.size(); i++) {
                    SideslipList foodList = new SideslipList();
                    food_img = list.get(i).getString("food_img");
                    food_name = list.get(i).getString("food_name");
                    food_price = list.get(i).getString("food_price");
                    foodList.setFood_img(food_img);
                    foodList.setFood_name(food_name);
                    foodList.setFood_price(food_price);
                    lxr.add(foodList);
                }
                ListAdapter_purch adapter = new ListAdapter_purch(lxr,purchase_record.this);
                lv.setAdapter(adapter);
            }
        });
    }




    @Override
    public void onReflash() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                select();//获取最新数据
                recreate();
                Toast.makeText(purchase_record.this,"刷新成功",Toast.LENGTH_SHORT).show();
            }
        }, 2000);
    }
}